<?php 
/*
 * @美潮风
 * @功能说明：图片上传服务
 * @更新说明：暂无更新
 * @文件名 UpLoad.class.php
 * @编码 UTF-8
 * @创建时间 2015-6-2 下午4:40:50
 * @创建人 XING
 */
namespace service\upload;
require SCRIPT_ROOT.'/../../library/uploads/JSON.php';
class UpLoad {
	/*
	 * 表单参数初始化
	 * @date: 2015-4-9上午10:05:40
	 * @editor: XING
	 */
	public function init($flod,$name,$dir='',$ext=''){
		$_tmpFile = $flod[$name];
		$outs = $upfiles = array();
		if($_tmpFile && is_array($_tmpFile['name'])){
			foreach($_tmpFile AS $key=>$vm){
				if($vm){
					foreach($vm AS $k=>$v){
						if(!$v || ($key=='error' && $v==6))
							continue;
						$outs[$k][$key] = $v;
					}
				}
			}
			if($outs){
				foreach($outs AS $vx){
					if (!empty($vx['error'])){
						continue;
					}
					
					$upfiles[] = self::uploadimg($vx,$dir,$ext);
				}
			}
		}else{
			$upfiles[] = self::uploadimg($_tmpFile,$dir,$ext);
		}
		
		return $upfiles;
	}
	
	/*
	 * 图片上传对外接口
	 * @date: 2015-4-9上午10:30:50
	 * @editor: XING
	 */
	public function upimgs($upload,$name,$dir='',$ext=''){
		return self::init($upload,$name,$dir,$ext);
	}
	
	/*
	 * 图片上传核心服务
	 * @date: 2015-4-9上午10:12:20
	 * @editor: XING
	 */
	public function uploadimg($files,$dir='',$ext=''){
		$base_path = $_SERVER['DOCUMENT_ROOT'] . '/../../statics/';
		$host_url = $_SERVER['HTTP_HOST'] . '/';
		
		$base_url = substr($host_url,strpos($host_url,'/'));
		
		//文件保存目录路径
		$save_path = $base_path . 'upload/';
		
		//文件保存目录URL
		$save_url = $base_url . 'upload/';
		
		//定义允许上传的文件扩展名
		$ext_arr = array(
				'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp', 'doc', 'docx', 'cvs', 'txt', 'pdf','xls'),
				'voice' => array('amr', 'wav', 'mp3')
			);
		//最大文件大小
		$max_size = 50000000;
		
		//PHP上传失败
		if (!empty($files['error'])) {
			exit('error:'.$files['error']);
		}
				
		//有上传文件时
		if (empty($_FILES) === false) {
			//原文件名
			$file_name = $files['name'];
			
			//服务器上临时文件名
			$tmp_name = $files['tmp_name'];
			//文件大小
			$file_size = $files['size'];
			
			//检查文件名
			if (!$file_name) {
				$error_tag = "请选择文件";
				self::codemsg($error_tag);exit;
			}

			//检查目录
			if (@is_dir($save_path) === false) {
				$error_tag = "上传目录不存在";
				self::codemsg($error_tag);exit;
			}
			//检查目录写权限
			if (@is_writable($save_path) === false) {
				$error_tag = "上传目录没有写权限";
				self::codemsg($error_tag);exit;
			}

			//检查目录名
			$dir_name = $dir ? trim($dir) : 'image';
			if (empty($ext_arr[$dir_name])) {
				$error_tag = "目录名不正确";
				self::codemsg($error_tag);exit;
			}
		
			//获得文件扩展名
			$temp_arr = explode(".", $file_name);
			if(count($temp_arr) < 2 || $dir == 'voice'){
				$file_ext = in_array($ext,$ext_arr['voice']) ? $ext : 'mp3';
			}else{
				$file_ext = array_pop($temp_arr);
				$file_ext = trim($file_ext);
				$file_ext = strtolower($file_ext);
				//检查扩展名
				if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
					$error_tag = "上传文件扩展名是不允许的扩展名,只允许" . implode(",", $ext_arr[$dir_name]) . "格式";
					self::codemsg($error_tag);exit;
				}
			}
			
			//检查是否已上传
			if (@is_uploaded_file($tmp_name) === false) {
				$error_tag = "上传失败";
				self::codemsg($error_tag);exit;
			}
			//检查文件大小
			if ($file_size > $max_size) {
				$error_tag = "上传文件大小超过限制";
				self::codemsg($error_tag);exit;
			}

			//创建文件夹
			if ($dir_name !== '') {
				$save_path .= $dir_name . "/";
				$save_url .= $dir_name . "/";
				if (!file_exists($save_path)) {
					mkdir($save_path);
				}
			}
		
			$ymd = date("Ymd");
			$save_path .= $ymd . "/";
			$save_url .= $ymd . "/";
			if (!file_exists($save_path)) {
				mkdir($save_path);
			}
			
			//新文件名
			$new_file_name = date("YmdHis") /* . '_' */ . rand(10000, 99999) . '.' . $file_ext;
			//移动文件
			$file_path = $save_path . $new_file_name;
			if (move_uploaded_file($tmp_name, $file_path) === false) {
				$error_tag = "上传文件失败";
				self::codemsg($error_tag);exit;
			}
			
			@chmod($file_path, 0644);
			$file_url = $save_url . $new_file_name;
			return $file_url;
			
		}
	}
	
	/*
	 * 状态码返回
	 * @date: 2015-4-9上午15:00:20
	 * @editor: XING
	 */
	 public function codemsg($codemsg){
		 echo $codemsg;exit;
	 }
	
	
	
}

?>